import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.set_headless()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome("../chromedriver-win64/chromedriver-win64/chromedriver.exe", chrome_options=options)
driver.implicitly_wait(10)

def newTable(urls, i):
    if len(urls) > i:
        window_handles = driver.window_handles
        # 切换到新标签页
        print(window_handles)
        new_tab = window_handles[-1]
        driver.switch_to.window(new_tab)
        setM3u8Status()
        driver.get(urls[i])
        while getM3u8FileStatus():
            time.sleep(5)
        i += 1
        print(i)
        newTable(urls, i)
        # login_btn = WebDriverWait(driver, 10, 0.5).until(EC.visibility_of_element_located((By.ID, "live-room")))
        # if login_btn:
        #     time.sleep(5)
        #     i += 1
        #     print(i)
        #     newTable(urls, i)



def getM3u8FileStatus():
    with open("m3u8Status.txt", "r", encoding="utf-8") as f:
        status = f.read()
    time.sleep(2)
    return "1" == status

def setM3u8Status():
    """0 表示新的请求等待 """
    with open("m3u8Status.txt", "w") as f:
        f.write("1")
    time.sleep(2)

with open("钉钉1.txt", "r", encoding="utf-8") as f:
    urls = f.readlines()
    driver.get(urls[0])
    while getM3u8FileStatus():
        time.sleep(5)
    newTable(urls, 1)
